# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=kopano-webapp
pkgdesc="WebApp for Kopano"
pkgver=5.2.0
pkgrel=4
# ppc64le and riscv64 blocked by libmdbx -> kopano-core
 arch="noarch !ppc64le !riscv64"
url="http://www.kopano.com/"
license="AGPL-3.0-only"
options="!check" # No test suite
depends="php7 php7-fpm php7-json php7-gettext php7-iconv php7-session php7-openssl php7-simplexml php7-soap"
# kopano-core necessary for mapi php module
makedepends="apache-ant openjdk8 kopano-core bash gettext-dev libxml2-utils php7-soap"
replaces="zarafa-webapp"
subpackages="
	kopano-webapp-contactfax:simple_plugin_splitfunc:noarch
	kopano-webapp-gmaps:simple_plugin_splitfunc:noarch
	kopano-webapp-pimfolder:simple_plugin_splitfunc:noarch
	kopano-webapp-src:_src:noarch
"
# 	 kopano-webapp-passwd:kopano_webapp_passwd_splitfunc

pkgusers="kopano-webapp"
pkggroups="kopano-webapp"

install="kopano-webapp.pre-install"

source="kopano-webapp-$pkgver.zip::https://stash.kopano.io/rest/api/latest/projects/KW/repos/kopano-webapp/archive?at=refs%2Ftags%2Fv$pkgver&format=zip
	nginx-location.conf
	php-fpm.example.conf
	kopano-webapp.conf
	kopano-webapp.ini
	compress-static
	0001-Fix-fatal-errors-in-translation-files.patch
"

#	0001-Remove-error-dev-stdout.patch"
# No passwd plugin for now because of no known good upstream and untested code
#kopano-webapp-passwd-$_pkgverpasswd.tar.gz::https://github.com/silentsakky/zarafa-webapp-passwd/archive/$_commithashpasswd.tar.gz

builddir="$srcdir/"

prepare() {
	olddir="$builddir"
	builddir="$srcdir/$pkgname"
	default_prepare
	builddir="$olddir"
}

#helper function
unpack() {
	local u
	verify
	initdcheck
	mkdir -p "$srcdir"
	local gunzip="$(command -v pigz || echo gunzip)"
	[ $gunzip = "/usr/bin/pigz" ] && gunzip="$gunzip -d"
	for u in $source; do
		local s
		local filename="$(filename_from_uri $u)"
		local new_root_dir="$builddir/${filename%%-[0-9]*}"
		if is_remote "$u"; then
			s="$SRCDEST/$filename"
		else
			s="$startdir/$u"
		fi
		case "$s" in
			*.tar)
				msg "Unpacking $s..."
				mkdir -p "$new_root_dir"
				tar -C "$new_root_dir" -xf "$s" ;;
			*.tar.gz|*.tgz)
				msg "Unpacking $s..."
				mkdir -p "$new_root_dir"
				$gunzip -c "$s" | tar -C "$new_root_dir" -x ;;
			*.tar.bz2)
				msg "Unpacking $s..."
				tar -C "$new_root_dir" -jxf "$s" ;;
			*.tar.lz)
				msg "Unpacking $s..."
				tar -C "$new_root_dir" --lzip -xf "$s" ;;
			*.tar.lzma)
				msg "Unpacking $s..."
				unlzma -T 0 -c "$s" | tar -C "$new_root_dir" -x  ;;
			*.tar.xz)
				msg "Unpacking $s..."
				local threads_opt
				if [ "$(readlink -f "$(command -v unxz)")" != "/bin/busybox" ]; then
					threads_opt="--threads=0"
				fi
				unxz $threads_opt -c "$s" | tar -C "$new_root_dir" -x ;;
			*.zip)
				msg "Unpacking $s..."
				unzip -n -q "$s" -d "$new_root_dir" ;;
		esac
	done
}

build() {
	# create translations, compress javascript-files
	cd kopano-webapp/
	ant deploy
	ant deploy-plugins
	# antdeploy-plugins leads to errors
	# execution one by one
	cd "$srcdir"
	for f in kopano-webapp-*; do
		if [ -d "$f" ]; then
			dest="kopano-webapp/plugins/${f//kopano-webapp-/}"
			test="$(find . -name "$f/$f-*" 2>/dev/null | head -n 1)"
			case "${f//kopano-webapp-/}" in
				spellchecker-languagepack-italian)
				dest=kopano-webapp/plugins/spellchecker-languagepack-italian-it
				;;
				files-backend-owncloud)
				dest=kopano-webapp/plugins/filesbackendOwncloud
				;;
				files-backend-smb)
				dest=kopano-webapp/plugins/filesbackendSMB
				;;
				fetchmail)
				mv "$f"/*/ "$dest"
				;;
				*)
				;;
			esac
			if [ -d "$test" ]; then
				mv "$test" "$dest"
			else
				mv "$f" "$dest"
			fi
			cd "$dest"*
			ant deploy -Droot-folder="$(pwd)/../../" -Dtarget-folder="$(pwd)/../../deploy/plugins"
			cd "$srcdir"
		fi
	done
}
package() {
	# application
	cd "$srcdir/kopano-webapp/deploy"
	install -dm 755 "$pkgdir/usr/share/webapps/$pkgname/"

	cp -r -- * "$pkgdir/usr/share/webapps/$pkgname/"
	cp "$srcdir/kopano-webapp/.htaccess" "$pkgdir/usr/share/webapps/$pkgname/"
	rm -R -- "$pkgdir/usr/share/webapps/$pkgname/plugins/"*

	# set version
	echo "$pkgver" > "$pkgdir/usr/share/webapps/$pkgname/version"
	## precompress for nginx
	find "$pkgdir/usr/share/webapps/$pkgname" -type f -exec "$srcdir/compress-static" "{}" \;
	## config examples
	install -dm 0750 -g kopano-webapp "$pkgdir/etc/webapps/$pkgname/"
	install -Dm 0644 -g kopano-webapp "$srcdir/php-fpm.example.conf" "$srcdir/nginx-location.conf" "$pkgdir/etc/webapps/$pkgname/"
	install -Dm 0644 -g kopano-webapp "$srcdir/kopano-webapp.conf" "$pkgdir/etc/webapps/$pkgname/apache.example.conf"
	install -Dm 0644 -g kopano-webapp "$srcdir/nginx-location.conf" "$pkgdir/etc/webapps/$pkgname/nginx.example.conf"
	## config mains
	install -Dm 0644 -g kopano-webapp config.php.dist "$pkgdir/etc/webapps/$pkgname/config.php"
	sed -i 's%define("DEBUG_DUMP_FILE", BASE_PATH . "debug.txt");%define("DEBUG_DUMP_FILE",	"/var/log/kopano-webapp/debug.txt");%g' debug.php.dist

	sed -i -e 's|\(\"DEBUG_LOADER\", \).*$|\1LOAD_RELEASE);|' debug.php.dist
	install -Dm 0644 debug.php.dist "$pkgdir/etc/webapps/$pkgname/debug.php.dist"
	ln -sf "/etc/webapps/$pkgname/config.php" "$pkgdir/usr/share/webapps/$pkgname/config.php"
	ln -sf "/etc/webapps/$pkgname/debug.php" "$pkgdir/usr/share/webapps/$pkgname/debug.php"
	## php
	install -Dm 0644 "$srcdir/$pkgname.ini" "$pkgdir/etc/php7/conf.d/$pkgname.ini"
	## php-fpm
	install -Dm 0644 "$srcdir/$pkgname.conf" "$pkgdir/etc/php7/php-fpm.d/$pkgname.conf"
	# /var/lib
	install -dm 0770 -g kopano-webapp "$pkgdir/var/lib/$pkgname/" "$pkgdir/var/lib/$pkgname/plugins" "$pkgdir/var/log/$pkgname"
	install -dm 0770 -g kopano-webapp "$pkgdir/var/lib/$pkgname/tmp"
	touch "$pkgdir/var/log/$pkgname/debug.txt"
	ln -sf "/var/log/$pkgname/debug.txt" "$pkgdir/usr/share/webapps/$pkgname/debug.txt"
	chown root:kopano-webapp "$pkgdir/usr/share/webapps/$pkgname"
	chmod 750 "$pkgdir/usr/share/webapps/$pkgname"
	chmod 660 "$pkgdir/var/log/$pkgname/debug.txt"
	chown root:kopano-webapp "$pkgdir/var/log/$pkgname/debug.txt"
	# fix access
	chmod 644 "$pkgdir/usr/share/webapps/kopano-webapp/client/kopano.js.map"
	# copy dompurify
	install -dm 755 "$pkgdir/usr/share/webapps/kopano-webapp/client/dompurify"
	# we're currently in srcdir/kopano-webapp/deploy
	install -Dm 644 ../client/dompurify/*.js* "$pkgdir/usr/share/webapps/kopano-webapp/client/dompurify/"
	chmod 0755 "$pkgdir/usr/share/webapps/$pkgname/"
}

simple_plugin_splitfunc() {
	depends="kopano-webapp"
	package_plugin
}
# FUNCTIONS
package_plugin() {
	: "${pluginname:="${subpkgname//kopano-webapp-/}"}"
	echo "Pluginname: $pluginname" >&2

	echo "Packaging $pkgname with pluginname $pluginname" >&2
	cd "$srcdir/kopano-webapp/deploy/plugins/$pluginname/"
	# /usr/share
	install -dm 755 "$subpkgdir/usr/share/webapps/kopano-webapp/plugins/$pluginname/"
	cp -R -- * "$subpkgdir/usr/share/webapps/kopano-webapp/plugins/$pluginname/"
	rm -f "$subpkgdir/usr/share/webapps/kopano-webapp/plugins/$pluginname/config.php"
	"$srcdir/compress-static" "$subpkgdir/usr/share/webapps/kopano-webapp/plugins/$pluginname/"
	# /var/lib
	install -dm 0700 -o kopano-webapp -g root "$subpkgdir/var/lib/kopano-webapp/plugins/$pluginname"
	# /etc
	if [ -e "config.php" ]; then
		## perform settings
		# convert windows line break to unix: http://stackoverflow.com/questions/11680815/removing-windows-newlines-on-linux-sed-vs-awk
		sed -i -e $'s/\r//' config.php
		install -dm 0750 -g kopano-webapp "$subpkgdir/etc/webapps/kopano-webapp/plugins/$pluginname/"
		## config mains
		install -m 0750 -g kopano-webapp config.php "$subpkgdir/etc/webapps/kopano-webapp/plugins/$pluginname/config.php"
		ln -sf "/etc/webapps/kopano-webapp/plugins/$pluginname/config.php" "$subpkgdir/usr/share/webapps/kopano-webapp/plugins/$pluginname/config.php"
		## config examples
		install -m 0750 "$subpkgdir/etc/webapps/kopano-webapp/plugins/$pluginname/config.php" "$subpkgdir/etc/webapps/kopano-webapp/plugins/$pluginname/config.example.php"
	fi
	if [ -e "$srcdir/$pluginname.ini" ];	then
	  ## php
	  mkdir -p "$subpkgdir/etc/php/conf.d"
	  cp "$srcdir/$pluginname.ini" "$subpkgdir/etc/php/conf.d"
	fi
}

_src() {
	pkgdesc="kopano-webapp (sources)"
	destdir="$subpkgdir/usr/share/src/"
	mkdir -p "$destdir"
	cp -R "$srcdir/kopano-webapp/" "$destdir"
	cd "$destdir/kopano-webapp/"
	ant clean
	# everything using the -src package requires tools. Saves some seconds at build time for those packages
	ant tools
}

sha512sums="
b81a19e90da8498f94033b76e3020215f152ec4cd1f86a0545964a26f9b8d8e2e68745a4ca70d9efa7ea73154e6ae8d40a9a7b718377541864ecfd1d72f51487  kopano-webapp-5.2.0.zip
849f08412c6754603cf6d31866ae0b257719530861fcb40fd6acc4530576da59c45792eecf0ff483de41398401a21f1b20e36f1183c8cfbb88053b1b8bd603f4  nginx-location.conf
bad70098b919c4cacce3152b276da489d7a36ca195b607508ad316211401dc98f07433ef505deb82a91388993e58b398cce2bc3eb295cf812362bae96b40fb35  php-fpm.example.conf
bdfe3960f88c776264e4caae47404a3647508f545f915a5aa205d486415d32c5ac89f8b4dbd9905a0b0751ec0ed42a2aef690cf8bca20f66e1f322141fad0f01  kopano-webapp.conf
062103e4ebf63c5dc4589d25e03f5ebe1df711613d5ea27a27265f3041832677ed384a16460e2bf8905c59f800abc84ef30a70512738c2cb335b76d275332ce8  kopano-webapp.ini
a9421b4206f06ec9e61a79fad5907b3616d030386585ee06af8fc20d376b2023e0ca65fa20ef290029d3a0eb523f6ef8ed3d7a8be48a5340285ed2f004d99f9c  compress-static
f4d77690324eff874c22ff92a3717cc719fd66c0295baf60213cd8fbaa59110a8c4ca238caae9c36d4e606cbe56c7462c18225f917aaebac42b45e6b7683f0df  0001-Fix-fatal-errors-in-translation-files.patch
"
